package app.tasks.schedule;

import app.env.Data;
import app.env.Position;
import app.agent.Schedule;
import jade.core.AID;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @Name: WaitMessage
 * @Description:
 * @Author: colfish
 * @Date: 2021/4/10 22:42
 * 好好学习，天天向上！
 */
public class WaitMessage extends OneShotBehaviour {
    private static final long serialVersionUID = -2850535188580377701L;

    private Logger logger = LogManager.getLogger(this.getClass());

    private Schedule schedule;


    int nextState;

    public WaitMessage(Schedule schedule){
        this.schedule = schedule;
        nextState = 1;
    }


    @Override
    public void action() {
        nextState = 1;
        ACLMessage signalReceived = schedule.receive();
        // 接收到了所有搜索代理发送的信号，对信号值排序
        if(schedule.receiveCount== Data.searchAgentPositions.size()){
            logger.info("Save completely!");
            // SortSignal
            nextState = 3;
        }
        // 判断是否已经找到了所有的目标代理
        if(Data.foundTargets.size() == Data.TARGET_NUMS){
            logger.info("Great, we've found all the target agents!");
            // 停止还在运动的搜索代理 StopSearch()
            nextState = 4;
        }
        if(signalReceived!=null){
            AID sender = signalReceived.getSender();
            schedule.tempSearchAID = sender;

            // 处理收到的字符串，用;分割，第一个字段为消息
            String[] content = signalReceived.getContent().split(";");

            String message = content[0];
            // 如果搜索代理没有从searchAgentPositions中删除，则执行下面逻辑
            if(Data.searchAgentPositions.containsKey(sender.getLocalName())) {
                if (message.equals(Data.FIND_TARGET)) {
                    // 若搜索代理发送的消息，则以";"分隔的字段，最后一个是搜索代理的位置
                    // 更新搜索代理位置
                    String[] searchCurPos = content[content.length - 1].split(",");
                    Data.searchAgentPositions.put(sender.getLocalName(), new Position(searchCurPos[0], searchCurPos[1]));
                    String[] tempTarPosition = content[1].split(",");
                    schedule.tempTarPos = new Position(tempTarPosition[0], tempTarPosition[1]);
                    // 接收到了搜索代理搜索到目标的消息, DealFindTarget()
                    nextState = 2;
                } else if (message.equals(Data.NOT_FIND_TARGET)) {
                    // 更新搜索代理位置
                    String[] searchCurPos = content[content.length - 1].split(",");
                    Data.searchAgentPositions.put(sender.getLocalName(), new Position(searchCurPos[0], searchCurPos[1]));
                    // 获取信号值
                    String signal = content[1];
                    Data.searchSignals.put(sender.getLocalName(), signal);
                    Data.receivedSearch.add(sender.getLocalName());
                    schedule.receiveCount++;
                }
            }
        }
    }

    @Override
    public int onEnd() {
        return nextState;
    }

}
